package smlab.utils
{
    import flash.display.*;
    import flash.geom.*;

	/**
	 * 碰撞检测
	 * @author simon
	 * 
	 */
    public class HitTest extends Object
    {

        public function HitTest()
        {
            return;
        }

        private static function getDrawMatrix(target:DisplayObject, hitRectangle:Rectangle, accuracy:Number) : Matrix
        {
            var _loc_4:Point = null;
            var _loc_5:Matrix = null;
            var _loc_6:* = target.root.transform.concatenatedMatrix;
            _loc_4 = target.localToGlobal(new Point());
            _loc_5 = target.transform.concatenatedMatrix;
            _loc_5.tx = _loc_4.x - hitRectangle.x;
            _loc_5.ty = _loc_4.y - hitRectangle.y;
            _loc_5.a = _loc_5.a / _loc_6.a;
            _loc_5.d = _loc_5.d / _loc_6.d;
            if (accuracy != 1)
            {
                _loc_5.scale(accuracy, accuracy);
            }
            return _loc_5;
        }

        public static function complexHitTestObject(target1:DisplayObject, target2:DisplayObject, accuracy:Number = 1) : Boolean
        {
            return complexIntersectionRectangle(target1, target2, accuracy).width != 0;
        }

        public static function complexIntersectionRectangle(target1:DisplayObject, target2:DisplayObject, accuracy:Number = 1) : Rectangle
        {
            if (accuracy <= 0)
            {
                throw new Error("ArgumentError: Error #5001: Invalid value for accuracy", 5001);
            }
            if (!target1.hitTestObject(target2))
            {
                return new Rectangle();
            }
            var _loc_4:* = intersectionRectangle(target1, target2);
            if (_loc_4.width * accuracy >= 1)
            {
            }
            if (_loc_4.height * accuracy < 1)
            {
                return new Rectangle();
            }
            var _loc_5:* = new BitmapData(_loc_4.width * accuracy, _loc_4.height * accuracy, false, 0);
            _loc_5.draw(target1, getDrawMatrix(target1, _loc_4, accuracy), new ColorTransform(1, 1, 1, 1, 255, -255, -255, 255));
            _loc_5.draw(target2, getDrawMatrix(target2, _loc_4, accuracy), new ColorTransform(1, 1, 1, 1, 255, 255, 255, 255), BlendMode.DIFFERENCE);
            var _loc_6:* = _loc_5.getColorBoundsRect(4294967295, 4278255615);
            _loc_5.dispose();
            if (accuracy != 1)
            {
                _loc_6.x = _loc_6.x / accuracy;
                _loc_6.y = _loc_6.y / accuracy;
                _loc_6.width = _loc_6.width / accuracy;
                _loc_6.height = _loc_6.height / accuracy;
            }
            _loc_6.x = _loc_6.x + _loc_4.x;
            _loc_6.y = _loc_6.y + _loc_4.y;
            return _loc_6;
        }

        public static function intersectionRectangle(target1:DisplayObject, target2:DisplayObject) : Rectangle
        {
            if (target1.root)
            {
            }
            if (target2.root)
            {
            }
            if (!target1.hitTestObject(target2))
            {
                return new Rectangle();
            }
            var _loc_3:* = target1.getBounds(target1.root);
            var _loc_4:* = target2.getBounds(target2.root);
            var _loc_5:* = new Rectangle();
            _loc_5.x = Math.max(_loc_3.x, _loc_4.x);
            _loc_5.y = Math.max(_loc_3.y, _loc_4.y);
            _loc_5.width = Math.min(_loc_3.x + _loc_3.width - _loc_5.x, _loc_4.x + _loc_4.width - _loc_5.x);
            _loc_5.height = Math.min(_loc_3.y + _loc_3.height - _loc_5.y, _loc_4.y + _loc_4.height - _loc_5.y);
            return _loc_5;
        }

    }
}
